<?php
/**
 * 小程序楼盘控制器
 */
namespace app\mini\controller;
class Building extends Pre
{
    public function lists(){
        $data = $this -> data;
        if ($data['key'])
            $findMap[] = ['name','like',"%$data[key]%"];
        $info = \app\common\model\Building::
        where($findMap)
            ->limit("$data[start],$data[count]")
            ->order('date desc')
            -> select()
            ->toArray();

        $loginInfo = $this -> loginInfo;
        //获取经纪人的所有上级
        $higher = [];
        $check = $loginInfo;
        while($check){
            $check = $check->higher;
            if ($check){
                $higherInfo = \app\common\model\Account::field('id,role')->find($check['a_id']);
                if ($higherInfo)
                    $higher[] = $higherInfo ->toArray();
            }

        }

        $url = 'https://sale.enterlo.com/index/index/single';
        foreach ($higher as $value){
            //房金
            if ($value['role'] == '21'){
                $url = 'https://fangsale.com/index/index/single';
                break;
            }
            elseif ($value['role'] == '30'){
                $url = 'https://luozhai.com/index/index/single';
                break;
            }
        }

        //添加二维码
        $code = new \app\common\logic\Qcode();
        foreach ($info as $k => $value){
//            if ($value['company'] =='罗宅网络科技有限公司'){
//                $url = 'https://luozhai.com/index/index/single';
//            }
//            elseif($value['company'] =='房金时代网络科技有限公司'){
//                $url = 'https://fangsale.com/index/index/single';
//            }
//            else{
//                $url = 'https://sale.enterlo.com/index/index/single';
//            }
            $urlRe = "$url?id=$value[id]&account_id=$loginInfo[id]";
            $fileName = "./static/upload/code/$loginInfo[id]$value[id].png";
            $code->createCode($urlRe,true,$fileName);
            $info[$k]['code'] = ltrim($fileName,'.');
        }


        foreach ($info as $k => $value){
            $info[$k]['design_img'] = showMobImg($value['design_img']);
        }

        $num = \app\common\model\Building::where($findMap)->count();
        return [
            'content' => $info,
            'num' => $num
        ];
    }

    public function detail(){
        $id = input('id');
        if ($id){
            $info = \app\common\model\Building::where('id',$id)->find()->toArray();
            $info['design_img'] = showMobImg($info['design_img']);

            $loginInfo = $this -> loginInfo;
            //获取经纪人的所有上级
            $higher = [];
            $check = $loginInfo;
            while($check){
                $check = $check->higher;
                if ($check)
                    $higher[] = \app\common\model\Account::field('id,role')->find($check['a_id'])->toArray();
            }
            $url = 'https://sale.enterlo.com/index/index/single';
            foreach ($higher as $value){
                //房金
                if ($value['role'] == '21'){
                    $url = 'https://fangsale.com/index/index/single';
                    break;
                }
                elseif ($value['role'] == '30'){
                    $url = 'https://luozhai.com/index/index/single';
                    break;
                }
            }
            //添加二维码
            $code = new \app\common\logic\Qcode();
//            if ($info['company'] =='罗宅网络科技有限公司'){
//                $url = 'https://luozhai.com/index/index/single';
//            }
//            elseif($info['company'] =='房金时代网络科技有限公司'){
//                $url = 'https://fangsale.com/index/index/single';
//            }
//            else{
//                $url = 'https://sale.enterlo.com/index/index/single';
//            }
            $url .= "?id=$id&account_id=$loginInfo[id]";
            $fileName = "./static/upload/code/$loginInfo[id].png";

            $code->createCode($url,true,$fileName);
            return [
                'content' => $info,
                'loginInfo' => $loginInfo,
                'code' =>     ltrim($fileName,'.')
            ];
        }
    }
}